﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Reflection;

namespace FriendFunny
{
    public static class GlobalAppExtensions
    {
        public static IHost Initialization(string[] args) 
        {
            var builder = Host.CreateDefaultBuilder(args);
            
            // 配置 Web 主机
            builder = builder.ConfigureWebHostDefaults(webHostBuilder =>
            {
                // 获取默认程序集名称
                var defaultAssemblyName = Assembly.GetAssembly(typeof(GlobalAppExtensions))?.GetName().Name;

                //  获取环境变量 ASPNETCORE_HOSTINGSTARTUPASSEMBLIES 配置
                var environmentVariables = Environment.GetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES");
                var combineAssembliesName = $"{defaultAssemblyName};{environmentVariables}";

                webHostBuilder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, combineAssembliesName);
            });

            // 构建主机
            var app = builder.Build();
            return app;
        }
    }
}
